<!--
 * @Author: [you name]
 * @Date: 2021-10-27 20:05:15
 * @LastEditors: [you name]
 * @LastEditTime: 2021-11-04 09:52:27
 * @Description: 
-->
<template>
  <div class="OrderPane">
    <el-table :data="resData.list">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="id" label="订单编号"></el-table-column>
      <el-table-column label="下单时间">
        <template v-slot="scope">
          {{ scope.row.orderTime | fmtDateTime }}
        </template>
      </el-table-column>
      <el-table-column prop="total" label="总价"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column
        prop="customer.username"
        label="顾客昵称"
      ></el-table-column>
      <el-table-column label="员工姓名">
        <template v-slot="scope">
          <el-tag v-if="scope.row.emmploy" type="success"
            >{{ scope.row.emmploy.realname }}
          </el-tag>
          <el-tag v-else type="info">暂未接单 </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template v-slot="{row}">
          <el-button type="text" @click="toDetailsHandler(row.id)">详情</el-button>
          <span style="padding:0 10px;" v-if="row.status == '待支付' || row.status == '待派单' || row.status == '待接单' ">
            <el-button type="text"  v-if="row.status == '待派单' " @click="toHandler(row.id)">派单</el-button>
            <el-button type="text" v-else-if="row.status == '待接单' " @click="cancleOrderHandler(row.id)">取消派单</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="resData.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="resData.size"
      layout="total, sizes, prev, pager, next"
      :total="resData.total"
    >
    </el-pagination>
    <!-- 模态框区域 -->
    <el-dialog title="派单" :visible.sync="visible">
    <el-radio-group v-model="waiterId">
    <el-radio v-for="(item, index) in usersData.list" :key="index" :label="item.id" border style="margin:10px">{{item.realname}}</el-radio>
    
  </el-radio-group>
  <div slot="footer" class="dialog-footer">
    <el-button @click="visible = false">取 消</el-button>
    <el-button type="primary" @click="toConfirmHandler">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>
<script>
import request from "@/utils/request";
import { mapState, mapActions } from 'vuex';
export default {
  props: {
    status: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      resData: {},
      queryPrams: {
        page: 1,
        pageSize: 10,
        status: this.status,
      },
      visible:false,
      waiterId:null,
    };
  },
  computed: {
    ...mapState('users', ['usersData'])
  },
  watch: {
    status(newVal,oldVal) {
      // console.log('状态',newVal,oldVal);
      this.queryPrams.status = newVal;
      this.pageQueryOrders();
    }
  },
  methods: {
    
    ...mapActions('users', ['pageQueryUsers']),
    
    // 分页查询并检索订单
    pageQueryOrders() {
      request
        .get("/order/pageQuery", {
          params: this.queryPrams,
        })
        .then((res) => {
          this.resData = res.data;
        });
    },
    // pageSize-change事件
    handleSizeChange(pageSize) {
      this.queryPrams.pageSize = pageSize;
      this.pageQueryOrders();
    },
    //  current-page-change事件
    handleCurrentChange(page) {
      this.queryPrams.page = page;
      this.pageQueryOrders();
    },
    // 派单按钮事件
    toHandler(orderId){
      this.visible = true ; //打开模态框
      this.orderId = orderId
    },
    // 派单模态框确定事件
    toConfirmHandler() {
      if(this.waiterId) {
        request.get('/order/sendOrder', {
          params: {
            waiterId: this.waiterId,
            orderId: this.orderId,
          },
        }).then(res => {
          this.pageQueryOrders() //重载订单数据
          this.visible = false //关闭模态框
          this.$message.success(res.message) //提示成功
        })
      }else{
        this.$message.warning('请选择员工')
      }
    },
    // 取消派单事件
    cancleOrderHandler(orderId) {
       this.$confirm('此操作取消派单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          request.get('/order/cancelSendOrder' , {
            params: {
              orderId
            }
          }).then(res => {
            this.pageQueryOrders() //重新加载数据
            this.$message.success(res.message)
          })
        })
    },
    // 详情事件
    toDetailsHandler(id) {
      this.$router.push({
        path: 'details',
        query: {
          id,
        }
      })
    }
  },
  created() {
    this.pageQueryOrders(); //分页检索订单
    this.pageQueryUsers({
      page: 1,
      pageSize: 100,
      rolrId:1
    }); //查询所有员工
  },
};
</script>